package com.kernel.kotlintest

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
import org.junit.jupiter.api.Test

class TestXml {
    @Test
    fun testXmlToObj() {
        val xmlStr = """
            <?xml version="1.0" encoding="UTF-8"?>
            <root>
                <fileFlag>123</fileFlag>
                <text>rootText</text>
                <BODY type="normal">
                    <mobile>33333333</mobile>
                    <list>
                        <item>
                            <name>新明1</name>
                        </item>
                        <item>
                            <name>新明2</name>
                            <text><![CDATA[<><><><><><><><><>]]></text>
                        </item>
                    </list>
                </BODY>
            </root>
        """.trimIndent()
        val builder = XmlMapper.builder()
        builder.serializationInclusion(JsonInclude.Include.NON_NULL)
        builder.propertyNamingStrategy(PropertyNamingStrategy())
        builder.enable(SerializationFeature.INDENT_OUTPUT)
        builder.enable(MapperFeature.USE_STD_BEAN_NAMING)
        builder.enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
        builder.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
        builder.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
        val xmlMapper = builder.build()
        val obj = xmlMapper.readValue(xmlStr, TestBean::class.java)
        obj.body?.type = "type"
        obj.body?.itemList?.add(TestBean.Item().apply {
            this.name = "new"
            this.value = "123"
            this.text = "http://127.0.0.1:8888/rest?param1=1&param2=2"
        })
        println(xmlMapper.writeValueAsString(obj))

        val json = """
            {
            "fileFlag":"2131231",
            "test":"test"
            }
        """.trimIndent()
        val builderJson = JsonMapper.builder()
        builderJson.serializationInclusion(JsonInclude.Include.NON_NULL)
        builderJson.propertyNamingStrategy(PropertyNamingStrategy())
        builderJson.enable(SerializationFeature.INDENT_OUTPUT)
        builderJson.enable(MapperFeature.USE_STD_BEAN_NAMING)
        builderJson.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
        builderJson.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
        val jsonMapper = builderJson.build()
        val objJson = jsonMapper.readValue(json, TestBean::class.java)
        objJson.body?.itemList?.add(TestBean.Item().apply {
            this.name = "new"
            this.value = "123"
            this.text = "http://127.0.0.1:8888/rest?param1=1&param2=2"
        })
        println(jsonMapper.writeValueAsString(objJson))
    }
}